//
//  WBStatusViewModel.swift
//  WB
//
//  Created by macssss on 16/7/6.
//  Copyright © 2016年 macssss. All rights reserved.
//

import Foundation

//单条微博视图模型
class WBStatusViewModel: CustomStringConvertible {
    
   //创建微博模型
    var status: WBDataModel
    //会员图标
    var  memberIcon: UIImage?
    //认证图标
    var ivpIcon: UIImage?
    //转发
    var retweetedStr: String?
    //评论
    var commentStr: String?
    //点赞
    var likeStr: String?
    
    //配图视图大小
    var pictureViewSize = CGSize()
    //被转发微博的文字
    var retweetedText: String?
    
    //预先计算行高
    var rowHeight: CGFloat = 0
    
    //如果是被转发的微博，原创一定没有图片
    var picUrls: [WBStatusPicture]? {
        
        
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    
    init(model: WBDataModel) {
        self.status = model
        
        if model.user?.mbrank > 0 && model.user?.mbrank < 7 {
            
            let imageName = "common_icon_membership_level\(model.user?.mbrank ?? 1)"
            
            memberIcon = UIImage(named: imageName)
        }
        
        switch model.user?.verified_type ?? -1 {
        case 0:
            ivpIcon = UIImage(named: "avatar_vip")
        case 2,3,5:
            ivpIcon = UIImage(named: "avatar_enterprise_vip")
        case 200:
            ivpIcon = UIImage(named: "avatar_grassroot")
        default:
            break
        }
        
        //设置底部计数
        retweetedStr = countString(count: model.reposts_count, defaultStr: "转发")
        commentStr = countString(count: model.comments_count, defaultStr: "评论")
        likeStr = countString(count: model.attitudes_count, defaultStr: "点赞")
        
        //计算视图的大小
        pictureViewSize = calcPictureViewSize(count:picUrls?.count)
        
        //设置被转发文字
        retweetedText = "@" + (status.retweeted_status?.user?.screen_name ?? "")
            + ":"
            + (status.retweeted_status?.text ?? "")

        //cell高度
        updateRowHeight()
    }
    
    var description: String {
        return status.description
        
    }
    
    //MARK: -  计算行高
    func updateRowHeight() {
        // 原创微博：顶部分隔视图(12) + 间距(12) + 图像的高度(34) + 间距(12) + 正文高度(需要计算) + 配图视图高度(计算) + 间距(12) ＋ 底部视图高度(35)
        // 被转发微博：顶部分隔视图(12) + 间距(12) + 图像的高度(34) + 间距(12) + 正文高度(需要计算) + 间距(12)+间距(12)+转发文本高度(需要计算) + 配图视图高度(计算) + 间距(12) ＋ 底部视图高度(35)
        let margin: CGFloat = 12
        let iconHeight: CGFloat = 34
        let toolbarHeight: CGFloat = 35
        
        var height: CGFloat = 0
        
        let viewSize = CGSize(width: UIScreen.cz_screenWidth() - 2 * margin, height: CGFloat(MAXFLOAT))
        let originalFont = UIFont.systemFont(ofSize: 15)
        let retweetedFont = UIFont.systemFont(ofSize: 14)
        
        //计算顶部位置
        height = 2 * margin + iconHeight + margin
        
        if let text = status.text {
            
            /**
             1> 预期尺寸，宽度固定，高度尽量大
             2> 选项，换行文本，统一使用 usesLineFragmentOrigin
             3> attributes: 指定字体字典
             */
            
            height += (text as NSString).boundingRect(with: viewSize,
                                                      options: [.usesLineFragmentOrigin],
                                                      attributes: [NSFontAttributeName: originalFont],
                                                      context: nil).height
            

        }
        
        //判断是不是转发微博
        if status.retweeted_status != nil {
            
            height += 2 * margin
            
            // 转发文本的高度 - 一定用 retweetedText，拼接了 @用户名:微博文字
            if let text = retweetedText {
                
                height += (text as NSString).boundingRect(with: viewSize,
                                                options: [.usesLineFragmentOrigin],
                                                attributes: [NSFontAttributeName: retweetedFont],
                                                context: nil).height
                
                
            }
            
        }
        
        //配图视图
        height += pictureViewSize.height
        
        height += margin
        
        //底部工具栏
        height += toolbarHeight
        
        //cell的高度是
        rowHeight = height

        
    }
    
    
//MARK: - 使用单张图片更新配图大小
     func updataSingleImageSize(image: UIImage) {
        
        //获得图片Size
        var size = image.size
        
        
        let maxWidth: CGFloat = 300
        let minWidth: CGFloat = 40
        
        // 过宽图像处理
        if size.width > maxWidth {
            // 设置最大宽度
            size.width = maxWidth
            // 等比例调整高度
            size.height = size.width * image.size.height / image.size.width
        }
        
        // 过窄图像处理
        if size.width < minWidth{
            size.width = minWidth
            
            // 要特殊处理高度，否则高度太大，会印象用户体验
            size.height = size.width * image.size.height / image.size.width / 4
        }

        
        
        size.height += WBStatusPictureViewOutterMargin
        
        pictureViewSize = size
        
        //更新行高
        updateRowHeight()
        
    }
    
    //MARK: - 配图视图大小
    private func calcPictureViewSize(count: Int?) -> CGSize {
        
        if count == 0 || count == nil  {
            return CGSize()
        }
        

        //计算行高度
        let row = (count! - 1) / 3 + 1
        
        var height = WBStatusPictureViewOutterMargin
        
        height += CGFloat(row) * WBStatusPictureItemWidth
        height += CGFloat(row - 1) * WBStatusPictureViewInnerMargin
        
        return CGSize(width: WBStatusPictureViewWidth, height: height)

        
    }
    
    
    //MARK: - 底部按钮 点赞数量
    private func countString(count: Int, defaultStr: String) -> String {
        
        if count == 0 {
            
            return defaultStr
        }
        
        if count < 10000 {
            
            return count.description
        }
        
        return String(format: "%02f 万", Double(count) / 100000)
    }
    
}








